package com.siwei.mes.controller.system;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.entity.system.SystemParam;
import com.siwei.mes.mapper.system.SystemParamMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 品控系统参数相关
 *
 * @author linzi
 * @since 2024-11-16 00:05:37
 */
@Api(value = "品控系统参数")
@RestController
@RequestMapping("systemParam")
public class SystemParamController {
    @Resource
    private SystemParamMapper systemParamMapper;
    @Value("${isOpenIronMark:false}")
    private Boolean isOpenIronMark;

    @ApiOperation(value = "获取所有系统参数")
    @GetMapping("getAllSystemParam")
    public Json getAllSystemParam() {
        List<SystemParam> systemParamList = systemParamMapper.selectAll();
        return Json.succ().data(ApiResult.addList(systemParamList));
    }

    @ApiOperation(value = "批量修改")
    @PostMapping("updateSystemParamList")
    public Json updateSystemParamList(@RequestBody SystemParam entity) {
        assertTrue(entity.getSystemParamList() == null, "系统参数集合为空");
        assertTrue(entity.getSystemParamList().isEmpty(), "系统参数集合为空");
        for (SystemParam systemParam : entity.getSystemParamList()) {
            systemParam.setUpdateTime(new Date());
            systemParamMapper.updateByPrimaryKeySelective(systemParam);
        }
        return Json.succ();
    }

    @AnonymousAccess
    @GetMapping("updateSystemParam")
    public Json updateSystemParam(@RequestParam("systemCode") String systemCode, @RequestParam("systemValue") String systemValue) {
        SystemParam systemParam = systemParamMapper.selectBySystemCode(systemCode);
        if (systemParam != null) {
            systemParam.setSystemValue(systemValue);
            systemParamMapper.updateByPrimaryKeySelective(systemParam);
        }
        return Json.succ().data(systemParam);
    }

    @ApiOperation("获取铁标是否开启")
    @GetMapping("/getIsIronMarkStatus")
    public Json getIsIronMarkStatus() {
        return Json.succ().data(ApiResult.addColumn("status", isOpenIronMark));
    }
}
